PROJECTENGRAM

[HO] 16 - GRAPHS

Graphs

Tags: #◇Computer Science #◇CCDSALG #◇Graphs #◇Conceptual #◇M6


◇Overview

Graphs consist of a set of vertices (nodes) and a set of edges that connect pairs of distinct vertices. The document differentiates between directed graphs (digraphs) and undirected graphs, noting that digraphs allow self-loops. It explains two primary methods for representing graphs in memory: adjacency lists and adjacency matrices.

◇Important Formulas

Graph Taxonomy & Handshaking Lemmas:

  • Undirected Handshaking Lemma:
    ∑v∈Vdeg(v)=2∣E∣\sum_{v \in V} \text{deg}(v) = 2|E|
    Corollary: In any undirected graph, the number of vertices with odd degree is always even.
  • Directed Graph Balance:
    ∑v∈Vin-degree(v)=∑v∈Vout-degree(v)=∣E∣\sum_{v \in V} \text{in-degree}(v) = \sum_{v \in V} \text{out-degree}(v) = |E|
  • Edge Density Bounds:
    • Undirected Simple Graph: 0≤∣E∣≤∣V∣(∣V∣−1)20 \le |E| \le \frac{|V|(|V| - 1)}{2}
    • Directed Graph: 0≤∣E∣≤∣V∣(∣V∣−1)0 \le |E| \le |V|(|V| - 1) (or ∣V∣2|V|^2 if self-loops permitted)

Representation Trade-off Matrix:

OperationAdjacency MatrixAdjacency List
Space ComplexityΘ(V2)\Theta(V^2)Θ(V+E)\Theta(V + E)
Edge Existence Query (u,v)(u, v)O(1)O(1)O(deg(u))O(\text{deg}(u))
Iterate All Neighbors of uuΘ(V)\Theta(V)Θ(deg(u))\Theta(\text{deg}(u))
Add VertexO(V2)O(V^2) (resize matrix)O(1)O(1) (amortized)
Add Edge (u,v)(u, v)O(1)O(1)O(1)O(1) (prepend to list)

◇Theoretical Framework: Memory Representations

Adjacency List Linked Layout

◇Worked Examples

  1. Adjacency Matrix Example: For the 4-vertex graph above:
    (0110001010010001)\begin{pmatrix} 0 & 1 & 1 & 0 \\ 0 & 0 & 1 & 0 \\ 1 & 0 & 0 & 1 \\ 0 & 0 & 0 & 1 \end{pmatrix}
  2. Dense vs. Sparse Selection: If ∣V∣=10,000|V| = 10,000 and ∣E∣=50,000|E| = 50,000 (sparse web graph), an Adjacency Matrix requires 10810^8 entries (≈100\approx 100 MB), while an Adjacency List requires 10,000+50,000=60,00010,000 + 50,000 = 60,000 references (≈1\approx 1 MB). Adjacency lists are strongly preferred for sparse graphs.

◇References & Navigation